<?php
class ChannelAction extends BaseAction{
	var $dao;
	var $isDel = false; //是否启用回收站
	function _initialize(){
		parent::_initialize();
		
		$this->checkAdmin();
		$this->dao = D('Channel','portal');
	}
	
	public function index(){
		$this->_getEdit();
		
		$map = $this->_getMap();
		$order = $this->_getOrder();		
		$list = $this->dao->where($map)->order($order)->findPage();

		$this->assign("count", $list['count']);
		$this->assign("page",  $list['html']);
		$this->assign("list",  $list['data']);
		
		$this->display('list');
	}
	
	function admin(){
		$daoP = D('Purview','portal');		
		
		$map   = $this->_getMap();
		$order = $this->_getOrder();
		$list  = $this->dao->where($map)->order($order)->findPage();
		$map['categoryId'] = array('exp','IS NULL');
		foreach ($list['data'] as &$v){
			$map['channelId'] = $v['id'];
			
			$uids = $daoP->where($map)->field('uid')->findall();
			foreach ($uids as $vo){
				$uidArr[] = $vo['uid']; 
			}
			
			$v['manager'] = implode(',', $uidArr);
			unset($uidArr);
		}

		$this->assign("count", $list['count']);
		$this->assign("page",  $list['html']);
		$this->assign("list",  $list['data']);
		
		$taglist = D('PortalTag')->getAllTag();
		$this->assign("taglist",  $taglist);
		
		$this->display();				
	}
	
	function add(){
		$this->_getPageTemp();
		
		$taglist = D('PortalTag')->getTopTag();
		$this->assign("taglist",  $taglist);		
		
		$this->display();
	}
	
	function edit(){
		$this->_getPageTemp();
		
		$res = $this->_getEdit();		
		if($res==false){
			$this->error('频道ID出错！');
		}
		
		$taglist = D('PortalTag')->getTopTag();
		$this->assign("taglist",  $taglist);
				
		$this->display();
	}
	
	function detail(){
		$id = intval($_REQUEST['id']);
		if(empty($id)){
			$this->error('参数出错！');
		}
		
		$cms = $this->dao->where("id='$id'")->find();
		$this->assign('cms', $cms);		
		
		$map['tree']     = $cms['tree'];
		$map['category'] = $cms['category'];
		$map['id']       = array('exp', '!='.$id);
		
		$list = $this->dao->where($map)->field('id,title')->limit(30)->findall();
		$this->assign('list', $list);
		
		//更新点击数
		$this->dao->where("id='$id'")->setInc('hits');

		$this->display();	
	}
   
	function _getEdit() {
		$id = $_REQUEST['id'];
		if(empty($id)){
			return false;
		}
		
		$edit	=	$this->dao->where('id='.$id)->find();		
		$this->assign('edit', $edit);
		
		return true;
	}
	
	function doDeal(){
		$map['domain']		=	t($_POST['domain']);
		$map['title']		=	t($_POST['title']);	
		//$map['manager']	    =	t($_POST['manager']);
		if(empty($map['title'])||empty($map['domain'])){
			$this->error('标题和域名不能为空！');
		}
		
		$map['type']		=	t($_POST['type']);
		$map['tags']	    =	t($_POST['tags']);
		$map['temp']	    =	t($_POST['temp']);
		$map['status']		=   t($_POST['status']);	

		$id = intval($_REQUEST['id']);
		if(empty($id)){
			$map['mtime'] =	time();
			$map['uid']   = $this->mid;
			$res = $this->dao->add($map);
			if($res){
				D('Page')->initAddPage($res, $map['domain']);
				D('PortalTag')->addTag($map['tags'], $res);
			}

			D('Log','admin')->addLog($this->mid, array('title'=>$map['title']), 'portal_channel_add');
		}else{
			$res = $this->dao->where("id='$id'")->save($map);
			D('Page')->where("channelId='$id'")->setField('channelDomain', $map['domain']);
			D('PortalTag')->addTag($map['tags'], $id);
			
			D('Log','admin')->addLog($this->mid, array('title'=>$map['title']), 'portal_channel_edit');
		}
		
		if($res){
			if(empty($id)) U('portal/Purview/index','channelId='.$res,true);
			else           U('portal/Channel/admin','',true);
		}else{
			$this->error('操作失败！');
		}
	}

	public function doDel(){
		//删除指定记录
		$id = intMember($_REQUEST['id']);
		if(empty($id)) {
			$this->error('非法操作');
		}

		$map['id'] = array('in', $id);	
		$res = $this->dao->where( $map )->delete();
		
		if($res){
			D('PortalTag')->delTag($id);
			U('portal/Channel/admin','',true);
		}else {
			$this->error("操作失败!");
		}
	}
	
	function temp(){
		$this->_getPageTemp();		
		$this->_getTempHtml();
		
		$this->display();
	}
	
	function doSetTempHtml(){
		$channel = t($_POST['channel']);
		$file    = t($_POST['file']);
		$dir = SITE_PATH."/data/portal/tpl/$channel/$file";
		//dump($dir);
		$html = $_POST['html'];
		
		//常量替换
        $replace =  array(
            APP_PUBLIC_PATH  => '../Public',// 项目公共目录
            WEB_PUBLIC_PATH  => '__PUBLIC__',// 站点公共目录
            APP_TMPL_PATH    => '__TMPL__',  // 项目模板目录
            __ROOT__         => '__ROOT__',       // 当前网站地址
            __APP__          => '__APP__',        // 当前项目地址
            __URL__          => '__URL__',        // 当前模块地址
            __ACTION__       => '__ACTION__',     // 当前操作地址
            __SELF__         => '__SELF__',       // 当前页面地址
        );
        $html = str_replace(array_keys($replace),array_values($replace),$html);

		$res = file_put_contents($dir, $html);
		if($res){
			D('Log','admin')->addLog($this->mid, '', 'portal_tpl');
		}
		$this->echoRes($res, '保存');
	}
	
	function _getTempHtml($channel='default', $file='index.html'){
		if(!empty($_GET['channel'])){
			$channel = t($_GET['channel']);
		}
		if(!empty($_GET['file'])){
			$file = t($_GET['file']);
		}
				
		$dir = SITE_PATH."/data/portal/tpl/$channel/$file";
		//dump($dir);exit;
		$html = file_get_contents($dir);
		$this->assign('html', $html);
	}

	function _getMap(){
		//ID
		if(isset($_REQUEST['id'])&&!empty($_REQUEST['id'])) {
			//$map['id'] = $_REQUEST['id'];
		}
		//标题
		if(isset($_REQUEST['title'])&&!empty($_REQUEST['title'])) {
			$map['title'] = array("like","%".$_REQUEST['title']."%");
		}
		//用户ID
		if(isset($_REQUEST['userId'])&&!empty($_REQUEST['userId'])) {
			$map['userId'] = $_REQUEST['userId'];
		}	
		//频道
		if(isset($_REQUEST['tree'])&&!empty($_REQUEST['tree'])) {
			$map['tree'] = $_REQUEST['tree'];
		}
		//分类
		if(isset($_REQUEST['category'])&&!empty($_REQUEST['category'])) {
			$map['category'] = $_REQUEST['category'];
		}
		//标签
		if(isset($_REQUEST['tagid'])&&!empty($_REQUEST['tagid'])) {
			$tagid = $_REQUEST['tagid'];
			$map['id'] = array('exp',"IN (SELECT aimId FROM `ts_portal_tag` where tagid=$tagid and type='channel')");
		}	
		
		return $map;	
	}
	
	function checkDomain(){
		$map['domain']    = t($_POST['domain']);
		if(!empty($_POST['id'])){
			$map['id'] = array('exp', '!='.$_POST['id']);
		}
		
		$id = D('Channel')->where($map)->getField('id');
		$this->_EchoRes($id);
	}	
	
	function _getPageTemp($type='pageTemp'){
		$dir = SITE_PATH."/data/portal/tpl/";
		mk_dir($dir);
		$d = dir($dir);
		
		while (false !== ($file = $d->read())) {			
			if ($file != "." && $file != ".." && $file != ".svn") {
				$arr[] = $file;
			}
		}
		$d->close();
		
		$this->assign('fileArr', $arr);
		
		return $arr;
	}
	
/*	function uploadTemp(){
		$this->assign('type', $_REQUEST['type']);
		
		$this->display();
	}
	
	function doUploadTemp(){
		$type = empty($_REQUEST['type']) ? 'pageTemp' : $_REQUEST['type'];
				
		$upload['allow_exts'] =	'html,htm';
		$upload['save_path']  = SITE_PATH."/data/portal/$type/".$this->channelId.'/';
		$upload['save_rule']  = '';
		
		$info		=	X('Xattach')->upload($type, $upload);
		if(!$info['status']){  //上传失败
            $this->error($info['info']);
		}else{
			$this->success('上传成功!');
		}		
	}	*/	

	function addAdmin(){
		$this->_initChannelId();
		$this->assign('channelId', $_GET['channelId']);

		$this->display();		
	}	
	function doAddAdmin(){	
		$this->_initChannelId();					
		$uids = explode(',', $_POST['admin_uids']);
		$data['channelId'] = $this->channelId;
		foreach ($uids as $uid){
			$data['uid'] = $uid;
			if(!empty($uid) && !D('Purview')->where($data)->find()){
				$res = D('Purview')->add($data);
			}
		}

		$this->echoRes($res,'增加专题管理员');		
	}
	function delAdmin(){
		$this->_initChannelId();
		$uid = $_GET['uid'];
		$map['uid'] = $uid;
		$map['channelId'] = $this->channelId;

		$res = D('Purview')->where($map)->delete();	
		//dump(D('Purview')->getLastSql());dump($res);exit;
		$this->echoRes($res,'删除专题管理员');
	}
}
?>